<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      chartInstance: null,
      //   allData: null, // 服务器返回的数据
      allData: [
        {
          name: "IPhone 11",
          stock: 2310,
          sales: 2103
        },
        {
          name: "长筒靴系带",
          stock: 34312,
          sales: 23509
        },
        {
          name: "打底毛衣",
          stock: 22140,
          sales: 12830
        },
        {
          name: "厚款羽绒服",
          stock: 10842,
          sales: 5492
        },
        {
          name: "牛仔裤",
          stock: 68102,
          sales: 44043
        },
        {
          name: "加厚卫衣",
          stock: 12032,
          sales: 8603
        },
        {
          name: "衬衫",
          stock: 9890,
          sales: 8960
        },
        {
          name: "HUAWEI",
          stock: 20130,
          sales: 12302
        },
        {
          name: "手机壳",
          stock: 89342,
          sales: 42948
        },
        {
          name: "打底裤",
          stock: 5034,
          sales: 1220
        }
      ],
      currentIndex: 0, // 当前显示的数据页数
      timerId: null
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timerId)
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.stock_ref, this.theme);
      const initOption = {
        title: {
          text: "▍库存销量分析",
          left: 20,
          top: 12
        },
        tooltip: {
          show: true,
          // backgroundColor: 'rgba(0,0,0,.8)',
          // borderColor: 'rgba(0,0,0,.8)',
          // textStyle: {
          //   color: '#fff'
          // },
        }
      };
      this.chartInstance.setOption(initOption);
      //   对图标进行鼠标事件监听
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData() {
      // 获取服务器的数据,对this.allData进行赋值后,调用updateChart方法更新图表
      // const { data: res } = await this.$http.get("stock");
      // this.allData = res;
      // console.log(this.allData);
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const centerArr = [
        ["18%", "40%"],
        ["50%", "40%"],
        ["82%", "40%"],
        ["34%", "75%"],
        ["66%", "75%"]
      ];
      const colorArr = [
        ["#4FF778", "#0BA82C"],
        ["#E5DD45", "#E8B11C"],
        ["#E8821C", "#E55445"],
        ["#5052EE", "#AB6EE5"],
        ["#23E5E5", "#2E72BF"]
      ];
      // 处理图表需要的数据
      const start = this.currentIndex * 5;
      const end = (this.currentIndex + 1) * 5;
      const showData = this.allData.slice(start, end);
      const seriesArr = showData.map((item, index) => {
        return {
          type: "pie",
          center: centerArr[index],
          hoverAnimation: false, // 关闭鼠标移入饼图的动画效果
          labelLine: {
            show: false // 隐藏指示线
          },
          label: {
            show: true,
            position: 'center',
            color: colorArr[index][0],
          },
          data: [
            {
              // name: item.name + '\n\n' + item.sales,
              value: item.sales,
              itemStyle: {
                // (x1,y1,x2,y2,[])
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  }
                ])
              },
              tooltip:{
                  formatter: `${item.name} <br/>销量：{c} <br/>占比：{d}%`,
              }
            },
            {
              name: item.name + '\n\n' + item.sales,
              value: item.stock,
              itemStyle: {
                color: '#ddd'
              },
              tooltip:{
                  formatter: `${item.name} <br/>库存：{c} <br/>占比：{d}%`,
              }
            }
          ]
        };
      });
      const dataOption = {
        series: seriesArr
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.stock_ref.offsetWidth / 100) * 3.6;
      const innerRadius = titleFontSize * 2;
      const outterRadius = innerRadius * 1.125;
      const adapterOption = {
        title: {
            textStyle:{
                fontSize: titleFontSize * 1.2
            }
        },
        series:[
            {
                type:'pie',
                radius:[outterRadius * 1.4, innerRadius * 1.1],
                label:{
                    fontSize: titleFontSize / 1.3
                }
            },
            {
                type:'pie',
                radius:[outterRadius * 1.4, innerRadius * 1.1],
                label:{
                    fontSize: titleFontSize / 1.3
                }
            },
            {
                type:'pie',
                radius:[outterRadius * 1.4, innerRadius * 1.1],
                label:{
                    fontSize: titleFontSize / 1.3
                }
            },
            {
                type:'pie',
                radius:[outterRadius * 1.4, innerRadius * 1.1],
                label:{
                    fontSize: titleFontSize / 1.3
                }
            },
            {
                type:'pie',
                radius:[outterRadius * 1.4, innerRadius * 1.1],
                label:{
                    fontSize: titleFontSize / 1.3
                }
            },
        ]
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    startInterval(){
        if(this.timerId){
            clearInterval(this.timerId)
        }
        this.timerId = setInterval(() => {
           this.currentIndex++
           if(this.currentIndex > 1){
               this.currentIndex = 0
           }
           this.updateChart()
        }, 3000)
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme(){
      this.chartInstance.dispose();//销毁当前的图表
      this.initChart(); // 重新以最新的主题名称初始化图表对象
      this.screenAdapter(); // 完成屏幕的适配
      this.updateChart(); // 更新图表的展示
    }
  }
};
</script>
<style scoped>

</style>
